import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Friends from '../views/Friends'
import Cart from '../views/Cart'
import Search from '../views/Search'
import NewsList from '../views/Home/news/NewsList'
import NewsInfo from '../views/Home/news/NewsInfo'
import PhotoList from '../views/Home/photos/PhotoList'
import PhotoInfo from '../views/Home/photos/PhotoInfo'
import GoodsList from '../views/Home/goods/GoodsList'
import GoodsInfo from '../views/Home/goods/GoodsInfo'
import storage from '../utils/storage'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: '登录',
      keepAlive: false,
      isShow: false
    }
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '首页',
      keepAlive: true,
      isShow: true
    }
  },
  {
    path: '/friends',
    component: Friends,
    meta: {
      title: 'friends',
      keepAlive: true,
      isShow: true
    }
  },
  {
    path: '/cart',
    component: Cart,
    meta: {
      title: 'cart',
      keepAlive: true,
      isShow: true

    }
  },
  {
    path: '/search',
    component: Search,
    meta: {
      title: 'search',
      isShow: true,
      keepAlive: true
    }
  },
  {
    path: '/home/newslist',
    component: NewsList,
    meta: {
      title: '新闻列表',
      keepAlive: true,
      isShow: false
    }
  },
  {
    path: '/home/newsinfo/:id',
    component: NewsInfo,
    // 允许参数通过props的方式接收
    props: true,
    meta: {
      title: '新闻详情',
      keepAlive: false,
      isShow: false
    }
  },
  {
    path: '/home/photolist',
    component: PhotoList,
    meta: {
      title: '图片列表',
      keepAlive: true,
      isShow: false
    }
  },
  {
    path: '/home/photoinfo/:id',
    component: PhotoInfo,
    // 允许参数通过props的方式接收
    props: true,
    meta: {
      title: '图片详情',
      keepAlive: false,
      isShow: false
    }
  },
  {
    path: '/home/goodslist',
    component: GoodsList,
    meta: {
      title: '商品',
      keepAlive: true,
      isShow: true
    }
  },
  {
    path: '/home/goodsinfo/:id',
    component: GoodsInfo,
    // 允许参数通过props的方式接收
    props: true,
    meta: {
      title: '商品详情',
      keepAlive: false,
      isShow: true
    }
  }

  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    const token = storage.getItem('token')
    console.log(token)
    if (token.length === 0) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
